package com.itbuka.goods.controller;


import com.github.pagehelper.Page;
import com.itbuka.entity.PageResult;
import com.itbuka.entity.Result;
import com.itbuka.entity.StatusCode;
import com.itbuka.goods.domain.Brand;
import com.itbuka.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Brand")
public class BrandController {
    @Autowired
    private BrandService iBrandService;

    /**
     * 查询全部
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Brand>> selectAll(){
        return new Result<>("查询成功",iBrandService.selectAll());
    }
    /**
     * 新增
     * @param iBrand
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Brand iBrand){
        iBrandService.insert(iBrand);
        return new Result<>("新增成功");
    }
    /**
     * 修改
     * @param iBrand
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Brand iBrand) {
        iBrandService.update(iBrand);
        return new Result<>("修改成功");
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        iBrandService.delete(ids);
        return new Result<>("删除成功");
    }
    /**
     * 品牌启用、停用 (status非null ，前端判断，null 后端判断)
     * @return
     */
    @GetMapping("/status")
    public Result status(@RequestParam Long id, @RequestParam Integer status) {
        if (id == null) {
            return new Result<>(false, StatusCode.ERROR, "查询不到品牌");
        }
        Integer integer = null;
        if (status == null) {
            integer = iBrandService.status(id);
        } else {
            integer = iBrandService.status(id, status);
        }
        if (integer != 1) {
            return new Result<>(false, StatusCode.ERROR, "品牌状态修改失败");
        }
        return new Result<>(true, StatusCode.OK, "品牌状态修改成功");
    }
    /**
     * 条件查询
     * @param iBrand
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<Brand>> select(@RequestBody Brand iBrand){
        return new Result<>("查询成功",iBrandService.selectList(iBrand));
    }
    /**
     * 品牌分页搜索实现(分页没传给个默认值)
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findPage(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Brand> pageList = iBrandService.findPage(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 品牌名称分页搜索实现
     * @param iBrand
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestParam Brand iBrand,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Brand> pageList = iBrandService.pageList(iBrand,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
    /**
     * 根据分类名称查询品牌列表
     * @param category
     * @return
     */
    @GetMapping("/category")
    public Result  findListByCategoryName(@RequestParam String category){
        System.out.println(category);
        List<Map> brandList = iBrandService.findListByCategoryName(category);
        return Result.ok("查询成功",brandList);
    }



}
